<?php

App::uses('TestEnvironment', 'Lib');
App::uses('FileSystem', 'Base.Lib');
App::uses('ConfigurationKeys', 'ConfigurationKeys.Lib');

class TestEnvironmentTest extends CakeTestCase {

    public $fixtures = array(
        'plugin.ConfigurationKeys.SettedConfigurationKey',
        'RawFile',
        'RawFileTag',
        'SourceDocument',
        'SourceDocumentTag',
        'Tag',
    );
    private $root;
    private $rawFilesDirectory;
    private $sharedFilesDirectory;

    public function setUp() {
        parent::setUp();

        $this->root = FileSystem::createTemporaryDirectory();
        $this->rawFilesDirectory = FileSystem::createTemporaryDirectory();
        $this->sharedFilesDirectory = FileSystem::createTemporaryDirectory();

        ConfigurationKeys::setKeyValue('raw_files_directory', $this->rawFilesDirectory);
        ConfigurationKeys::setKeyValue('shared_files_directory', $this->sharedFilesDirectory);

        $RawFile = ClassRegistry::init('RawFile');

        for ($i = 0; $i < 10; $i++) {
            $RawFile->create();
            $this->assertNotEquals($RawFile->save(
                    array(
                        'RawFile' => array(
                            'name' => 'raw_file_' . $i
                        )
                    )
                ), false);
        }

        $Tag = ClassRegistry::init('Tag');

        for ($i = 0; $i < 10; $i++) {
            $Tag->create();
            $this->assertNotEquals($Tag->save(array(
                    'Tag' => array(
                        'name' => 'tag_' . $i
                    )
                    )
                ), false);
        }


        $RawFile->clearCache();
        $Tag->clearCache();

        $RawFileTag = ClassRegistry::init('RawFileTag');

        foreach ($Tag->find('all') as $tag) {
            foreach ($RawFile->find('all') as $rawFile) {
                $RawFileTag->create();
                $this->assertNotEquals($RawFileTag->save(array(
                        'RawFileTag' => array(
                            'raw_file_id' => $rawFile['RawFile']['id'],
                            'tag_id' => $tag['Tag']['id'],
                        )
                        )
                    ), false);
            }
        }
        
        $RawFileTag->clearCache();
    }

    public function tearDown() {

        FileSystem::recursiveRemoveDirectory($this->root);
        FileSystem::recursiveRemoveDirectory($this->sharedFilesDirectory);
        FileSystem::recursiveRemoveDirectory($this->rawFilesDirectory);
        parent::tearDown();
    }

    public function testCreate() {
        $env = new TestEnvironment($this->root);

        $this->assertEqual(
            FileSystem::listFiles($env->getRawFilesDirectory())
            , FileSystem::listFiles($this->rawFilesDirectory)
        );

        $this->assertEqual(
            FileSystem::listFiles($env->getSharedFilesDirectory())
            , FileSystem::listFiles($this->sharedFilesDirectory)
        );
    }

}